/*
 * Copyright (C) 2013 by KB3ZYT Rich Nash
 * 
 * Module modified after being inheritted from EA5HAV. The same license described below
 * is in effect.
 *
 * The below copyright is kept intact.
 *
 */

/* Credit to the trackuino project written by EA5HAV Javi
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "config.h"
#include "ax25.h"
#include "aprs.h"
#include <Arduino.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#define MAXSENDBUFFER 250

void latToStr(char *s, float lat)
{
    int deg;
    int min;
    int minTenths;
    
    deg = (int)lat;
    lat = (lat - (float)deg) * 60.0f;
    min = (int)lat;
    lat = (lat - (float)min) * 100.0f;
    minTenths = (int)(lat + 0.5); // Round the tenths
    snprintf(s, 9, "%02d%02d.%02dN", deg, min, minTenths); 
}

void lonToStr(char *s, float lon)
{
    int deg;
    int min;
    int minTenths;
    
    lon = -lon; // Assume we are in the western hemisphere
    deg = (int)lon;
    lon = (lon - (float)deg) * 60.0f;
    min = (int)lon;
    lon = (lon - (float)min) * 100.0f;
    minTenths = (int)(lon + 0.5); // Round the tenths
    snprintf(s, 10, "%03d%02d.%02dW", deg, min, minTenths); 
}

// Exported functions
void aprs_send(uint8_t dayOfMonth, uint8_t hour, uint8_t min
               ,float lat, float lon // degrees
               ,float altitude // meters
               ,uint8_t coarse // degrees
               ,uint16_t speed
               ,float externalTemp, float externalHumidity
               ,float internalTemp, float barametricPressure
               ,char *comment)
{
    char temp[12];                   // Temperature (int/ext)
    const struct s_address addresses[] = { 
        {D_CALLSIGN, D_CALLSIGN_ID},  // Destination callsign
        {S_CALLSIGN, S_CALLSIGN_ID},  // Source callsign (-11 = balloon, -9 = car)
#ifdef DIGI_PATH1
        {DIGI_PATH1, DIGI_PATH1_TTL}, // Digi1 (first digi in the chain)
#endif
#ifdef DIGI_PATH2
        {DIGI_PATH2, DIGI_PATH2_TTL}, // Digi2 (second digi in the chain)
#endif
    };
    
    uint8_t buf[MAXSENDBUFFER];
    ax25_initBuffer(buf,MAXSENDBUFFER);
    ax25_send_header(addresses, sizeof(addresses)/sizeof(s_address));
    ax25_send_byte('/');                // Report w/ timestamp, no APRS messaging. $ = NMEA raw data
    snprintf(temp, 8, "%02d%02d%02dz", (int)dayOfMonth, (int)hour, (int)min); 
    ax25_send_string(temp);  // Course (degrees)
    latToStr(temp, lat);
    ax25_send_string(temp);             // Lat:
    ax25_send_byte('/');                // Symbol table
    lonToStr(temp, lon);
    ax25_send_string(temp);     // Lon: 000deg and 25.80 min
    ax25_send_byte('v');                // Symbol: O=balloon, -=House, v=Blue Van, >=Red Car
    snprintf(temp, 4, "%03d", (int)(0 + 0.5)); 
    ax25_send_string(temp);             // Course (degrees)
    ax25_send_byte('/');                // and
    snprintf(temp, 4, "%03d", (int)(0 + 0.5));
    ax25_send_string(temp);             // speed (knots)
    ax25_send_string("/A=");            // Altitude (feet). Goes anywhere in the comment area
    snprintf(temp, 7, "%06ld", (long)(altitude / 0.3048));      // 10000 ft = 3048 m
    ax25_send_string(temp);
    ax25_send_string(comment);     // Comment
    ax25_send_footer();
    ax25_flush_frame();                 // Tell the modem to go
}
